<template>
  <ApplianceSettingsView
    v-if="loaded"
    :api-control="apiControl"
    :ota-helper="null"
    @to-param-settings="toParameterSettings"
  />
</template>

<script setup lang="ts">
import ApplianceSettingsView, {
  type ApiControl,
  type ToParameterSettingsPayload,
} from '@/pages/appliance/settings/applianceSettingsView.vue'
import { onLoad } from '@dcloudio/uni-app'
import type { DemoDatasource, DatasourceMap } from '@/demo/types'
import type { ApplianceDetail, ApplianceSimpleSetting } from '@/api/appliance'
import { toPage } from '@/utils/mpHelper'
import { mock } from '../utils'
import { getSettingGroups } from '@/api/appliance'
import { loadFileModule } from '@/controller/demo/utils'

let demoDatasource: DemoDatasource | null = null

const apiControl: ApiControl = {
  getApplianceDetail: mock(() => (demoDatasource?.detail || {}) as ApplianceDetail),
  getAllSetting: mock(() => ({ settings: demoDatasource?.settings || [] })),
  getFeatures: mock(() => ({ commands: [], events: [], options: [], programs: [], settings: [], status: [] })),
  getSimpleSetting: async () => {
    const { DemoSimpleSettings } = await loadFileModule<{ DemoSimpleSettings: ApplianceSimpleSetting[] }>(
      '/demo/BIWD/data'
    )
    return mock(() => ({ settings: DemoSimpleSettings }))()
  },
  getGuest: mock(() => ({ guests: [] })),
  deregister: mock(),
  changeApplianceName: mock(),
  setSetting: mock(),
  setHomeApplianceSettings: mock(),
  setHVNHomeApplianceSettings: mock(),
  getAppSupportCommands: mock(() => ({ commands: demoDatasource?.commands || [] })),
  setUserAddition: mock(),
  getSettingGroups,
  renameRoomName: mock(),
  getStatus: mock(),
  getSetting: mock(),
}

const loaded = ref(false)
onLoad(async (options) => {
  const { DemoDatasourceMap } = await loadFileModule<{ DemoDatasourceMap: DatasourceMap }>('/demo/map')
  const type = options?.type || ''
  demoDatasource = DemoDatasourceMap[type]
  loaded.value = true
})

/**
 * 跳转参数设置
 */
function toParameterSettings(payload: ToParameterSettingsPayload) {
  toPage('/pages/appliance/Demo/settings/parameterSettings', payload)
}
</script>
